package com.SmartCare.controller.common;

import com.SmartCare.common.Result;
import com.SmartCare.service.SmsService;
import com.SmartCare.utils.SMSUtils;
import com.SmartCare.utils.ValidateCodeUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

@RestController
@Api(tags = "短信服务接口")
@Slf4j
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private SmsService smsService;
    //测试短信功能：请求URL GET http://localhost:8080/sendMsg?phone=xxx
    @GetMapping ("/sendMsg")
    public Result sendMsg(String phone, HttpSession session) {
        if(StringUtils.isNotEmpty(phone)){
            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(6).toString();

            //调用阿里云API完成发送短信
           SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone,code);
            //将生成的验证码保存到Session
            session.setAttribute(phone,code);
            return Result.success();
        }
        return Result.error();
    }


    //获取验证码
    @GetMapping("/getAuthCode")
    public Result getAuthCode(String phone) {
        return smsService.getAuthCode(phone);
    }

    //后台获取注册验证码
    @GetMapping("/manager/sign-code")
    public Result sendSignCode(@RequestParam String phone) {
        return smsService.managerSendCode(phone, "SIGN");
    }

    //后台验证注册验证码
    @GetMapping("/manager/verify-sign")
    public Result verifyManagerSign(@RequestParam String phone, @RequestParam String code) {
        return smsService.managerVerifyCode(phone, code, "SIGN");
    }

    //后台获取登录验证码
    @GetMapping("/manager/login-code")
    public Result sendLoginCode(@RequestParam String phone) {
        return smsService.managerSendCode(phone, "LOGIN");
    }

    //后台验证登录验证码
    @GetMapping("/manager/verify-login")
    public Result verifyManagerLogin(@RequestParam String phone, @RequestParam String code) {
        return smsService.managerVerifyCode(phone, code, "LOGIN");
    }

    //后台获取修改密码验证码
    @GetMapping("/manager/change-code")
    public Result sendChangeCode(@RequestParam String phone) {
        return smsService.managerSendCode(phone, "CHANGE");
    }


    //后台验证修改密码验证码
    @GetMapping("/manager/verify-change")
    public Result verifyManagerChange(@RequestParam String phone, @RequestParam String code) {
        return smsService.managerVerifyCode(phone, code, "CHANGE");
    }


}
